$(function(){
	new PACK();
});

function PACK(){
	this.obj_table=$("table.tab-display");
	this.order_id=$("table.tab-display #order_id").val();
	this.ss_id=$("#ss_id").val();
	this.obj_addbtn=this.obj_table.find("input[value='添加']");
	this.obj_delbtn=this.obj_table.find("input[value='移除']");
	this.span_box_cnt_sum=this.obj_table.find("#box_cnt_sum");
	this.span_total_cnt_sum=this.obj_table.find("#total_cnt_sum");
	this.span_gross_weight_sum=this.obj_table.find("#gross_weight_sum");
	this.bind();
}

PACK.prototype.__bind=function(fn,me){
	return function(){
		return fn.apply(me,arguments);
		};
};

PACK.prototype.bind=function(){
	this.obj_addbtn.bind("click",this.__bind(this.on_addbtn_click,this));
	this.obj_delbtn.bind("click",this.__bind(this.on_delbtn_click,this));
	this.obj_table.find("input[name='size_v1[]'],input[name='size_v2[]'],input[name='size_v3[]'],input[name='size_v4[]'],input[name='size_v5[]'],input[name='size_v6[]']").bind("change",this.__bind(this.on_size_change,this));
	this.obj_table.find("input[name='box_cnt[]'],input[name='amount_per_box[]']").bind("change",this.__bind(this.on_cnt_change,this));
	this.obj_table.find("input[name='gross_weight[]']").bind("change",this.__bind(this.on_gross_weight_change,this));	
	this.obj_table.find("input[name='total_cnt[]']").bind("change",this.__bind(this.on_total_cnt_change,this));
};

PACK.prototype.on_addbtn_click=function(evt){
	var element=evt.srcElement||evt.target;
	var data=this.get_row_data($(element));
	if(data['color']=='')
	{
		alert("颜色不能为空!");
		return;
	}
	if(data['box_num']=='')
	{
		alert("箱号不能为空!");
		return;
	}
	if(data['box_cnt']<1)
	{
		alert("箱数不能少于1!");
		return;
	}
	this.ajax_add_data(data);

};

PACK.prototype.on_delbtn_click=function(evt){
	if(confirm('确认移除此装箱单信息吗?')==true)
	{
	var element=evt.srcElement||evt.target;
	$(element).parents("tr").remove();
		this.set_sum_info();
	}

};

//当尺寸数量变动时变动相关数据
PACK.prototype.on_size_change=function(evt){
	var element=evt.srcElement||evt.target;
	var tr=$(element).parents("tr");
	var size_v1=tr.find("[name='size_v1[]']").val();
	size_v1=size_v1!=''?parseFloat(size_v1):0;
	var size_v2=tr.find("[name='size_v2[]']").val();
	size_v2=size_v2!=''?parseFloat(size_v2):0;
	var size_v3=tr.find("[name='size_v3[]']").val();
	size_v3=size_v3!=''?parseFloat(size_v3):0;
	var size_v4=tr.find("[name='size_v4[]']").val();
	size_v4=size_v4!=''?parseFloat(size_v4):0;
	var size_v5=tr.find("[name='size_v5[]']").val();
	size_v5=size_v5!=''?parseFloat(size_v5):0;
	var size_v6=tr.find("[name='size_v6[]']").val();
	size_v6=size_v6!=''?parseFloat(size_v6):0;
	var all_cnt=size_v1+size_v2+size_v3+size_v4+size_v5+size_v6;
	tr.find("[name='amount_per_box[]']").val(all_cnt);
	tr.find("[name='amount_per_box[]']").change();
};

//当每箱数量和箱数变动时变动相关数据
PACK.prototype.on_cnt_change=function(evt){
	var element=evt.srcElement||evt.target;
	var tr=$(element).parents("tr");
	var box_cnt=tr.find("[name='box_cnt[]']").val();
	box_cnt=box_cnt!=''?parseFloat(box_cnt):0;
	var amount_per_box=tr.find("[name='amount_per_box[]']").val();
	amount_per_box=amount_per_box!=''?parseFloat(amount_per_box):0;
	var all_cnt=box_cnt*amount_per_box;
	tr.find("[name='total_cnt[]']").val(all_cnt);
	this.set_sum_info();
};

//当累计件数变动时变动统计数据
PACK.prototype.on_total_cnt_change=function(evt){
	var element=evt.srcElement||evt.target;
	var tr=$(element).parents("tr");
	this.set_sum_info();
};

//当毛重变动时变动统计数据
PACK.prototype.on_gross_weight_change=function(evt){
	var element=evt.srcElement||evt.target;
	var tr=$(element).parents("tr");
	this.set_sum_info();
};

PACK.prototype.ajax_add_data=function(data){
	var url="index.php?app=order&act=ajax_add_packinfo&order_id="+this.order_id+"&ss_id="+this.ss_id;
	var json_data=new Array();
	for(var i in data)
	{
		json_data.push(i+'='+encodeURI(data[i]));
	}
	json_data=json_data.join("&");
	$.ajax({
		async:false,
		url:url,
		data:json_data,
		type:"POST",
		dataType:"json",
		success:this.__bind(function(ret){
			if(ret.done)
			{
				alert(ret.msg);
				var add_tr=this.obj_table.find("tr.add_row");
				var new_tr=add_tr.clone();
				this.obj_table.find("tr.edit_row").size()>0?this.obj_table.find("tr.edit_row:last").after(new_tr):this.obj_table.find("tr.add_row:last").after(new_tr);
				new_tr.find("[name='pid[]']").val(ret.retval.pid);
				new_tr.find("[name='color[]']").val(data['color']);
				new_tr.find("[name='last_box[]']").val(data['last_box']);
				var del_btn=new_tr.find("[name='op[]']").val("移除");
				del_btn.bind("click",this.__bind(this.on_delbtn_click,this));
				new_tr.removeClass("add_row","edit_row");
				new_tr.addClass("edit_row");
				add_tr.find("input[type='text']:visible,select:visible").val("");
				new_tr.find("input[name='size_v1[]'],input[name='size_v2[]'],input[name='size_v3[]'],input[name='size_v4[]'],input[name='size_v5[]'],input[name='size_v6[]']").bind("change",this.__bind(this.on_size_change,this));
				new_tr.find("input[name='box_cnt[]'],input[name='amount_per_box[]']").bind("change",this.__bind(this.on_cnt_change,this));
				new_tr.find("input[name='gross_weight[]']").bind("change",this.__bind(this.on_gross_weight_change,this));
				new_tr.find("input[name='total_cnt[]']").bind("change",this.__bind(this.on_total_cnt_change,this));
				this.set_sum_info();
				return true;
			}
			else
			{
				alert(ret.msg);
				return false;
			}
		},this)
		
	});
};

//获取行数据,参数为行内的某一元素
PACK.prototype.get_row_data=function(obj){
	var row=obj.parents("tr");
	var data=new Array();
	data['box_num']=$("[name='box_num[]']").val();
	data['color']=$("[name='color[]']").val();
	data['last_box']=$("[name='last_box[]']").val();
	data['size_v1']=$("[name='size_v1[]']").val();
	data['size_v2']=$("[name='size_v2[]']").val();
	data['size_v3']=$("[name='size_v3[]']").val();
	data['size_v4']=$("[name='size_v4[]']").val();
	data['size_v5']=$("[name='size_v5[]']").val();
	data['last_box']=$("[name='last_box[]']").val();
	data['size_v6']=$("[name='size_v6[]']").val();
	data['amount_per_box']=$("[name='amount_per_box[]']").val();
	data['box_cnt']=$("[name='box_cnt[]']").val();
	data['total_cnt']=$("[name='total_cnt[]']").val();
	data['gross_weight']=$("[name='gross_weight[]']").val();
	return data;
};

//自动计算统计数据
PACK.prototype.set_sum_info=function(){
	var rows=this.obj_table.find("tr.edit_row,tr.add_row");
	var box_cnt_sum=0;
	var total_cnt_sum=0;
	var gross_weight_sum=0;
	rows.each(function(){
		var box_cnt=$(this).find("[name='box_cnt[]']").val();
		box_cnt=box_cnt!=''?parseFloat(box_cnt):0;
		var gross_weight=$(this).find("[name='gross_weight[]']").val();
		gross_weight=gross_weight!=''?parseFloat(gross_weight):0;
		var total_cnt=$(this).find("[name='total_cnt[]']").val();
		total_cnt=total_cnt!=''?parseFloat(total_cnt):0;
		box_cnt_sum+=box_cnt;
		total_cnt_sum+=total_cnt;
		gross_weight_sum+=gross_weight;
	});
	this.span_box_cnt_sum.html(box_cnt_sum);
	this.span_total_cnt_sum.html(total_cnt_sum);
	this.span_gross_weight_sum.html(gross_weight_sum);
	return true;
};